package pl.pyjter;

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import java.awt.BorderLayout;
import java.util.HashMap;

import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LibApp {

	private JFrame frame;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LibApp window = new LibApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				} catch (Throwable e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws Throwable 
	 */
	public LibApp() throws Throwable {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws Throwable 
	 */
	private void initialize() throws Throwable {
		HashMap<String, Class> tables = new HashMap<>();
		tables.put("Book", Book.class);
		tables.put("Author", Author.class);
		tables.put("Publisher", Publisher.class);
		
		final LibraryEngine engine = new LibraryEngine(tables);
		
		frame = new JFrame();
		frame.setBounds(100, 100, 700, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JSplitPane splitPane = new JSplitPane();
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JScrollPane tablesPane = new JScrollPane();
		splitPane.setLeftComponent(tablesPane);
		
		DefaultListModel listModel = new DefaultListModel();
		final JList tablesList = new JList(listModel);
		
		tablesList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				table.setModel(engine.getTableModel(tablesList.getSelectedValue().toString()));
			}
		});
		
		for(String tableName : engine.getTables().keySet()) {
			listModel.addElement(tableName);
		}
		tablesPane.setViewportView(tablesList);
		
		JScrollPane tableModelPane = new JScrollPane();
		
		splitPane.setRightComponent(tableModelPane);
		
		table = new JTable();
		tableModelPane.setViewportView(table);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnTable = new JMenu("Table");
		menuBar.add(mnTable);
		
		JMenuItem mntmInsert = new JMenuItem("Insert");
		mnTable.add(mntmInsert);
		
		JMenuItem mntmUpdate = new JMenuItem("Update");
		mnTable.add(mntmUpdate);
		
		JMenuItem mntmDelete = new JMenuItem("Delete");
		mntmDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int tableIndex = tablesList.getSelectedIndex();
				int rowIndex = table.getSelectedRow();
				
				if((tableIndex > -1) && (rowIndex > -1)) {
					System.out.println(table.getValueAt(table.getSelectedRow(), 0));
					System.out.println(tablesList.getSelectedValue().toString());
					engine.deleteRow(tablesList.getSelectedValue().toString(), (int) table.getValueAt(rowIndex, 0),
							rowIndex);
				}
				
				table.clearSelection();
				table.updateUI();
			}
		});
		mnTable.add(mntmDelete);
	}

}
